<template>
  <div class="order-page">
    <!-- 顶部导航 -->
    <el-breadcrumb separator="/" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>酒店</el-breadcrumb-item>
      <el-breadcrumb-item>订单填写</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 主体 -->
    <div class="order-container">
      <!-- 左侧 -->
      <div class="left-panel">
        <el-card shadow="hover" class="hotel-info">
          <div class="hotel-img-wrapper">
            <img :src="hotel.hotelImg" alt="hotel" class="hotel-img" />
          </div>
          <div class="hotel-details">
            <h2>{{ hotel.hotelName }}</h2>
            <p>{{ hotel.address }}</p>
          </div>
        </el-card>

        <!-- 房型列表 -->
        <div v-for="(room, index) in rooms" :key="room.id" class="room-card">
          <el-collapse v-model="room.active">
            <el-collapse-item :title="room.type">
              <el-form label-width="100px">
                <el-form-item label="房间数">
                  <el-input-number v-model="room.quantity" :min="1" />
                </el-form-item>
                <el-form-item label="入住人数">
                  <el-input-number v-model="room.guests" :min="1" :max="room.maxGuests" />
                </el-form-item>

                <!-- 多名入住人姓名与身份证 -->
                <div v-for="n in room.guests" :key="n" class="guest-info">
                  <h4>入住人 {{ n }}</h4>
                  <el-form-item label="姓名">
                    <el-input v-model="room.guestList[n-1].name" placeholder="姓名" />
                  </el-form-item>
                  <el-form-item label="身份证">
                    <el-input v-model="room.guestList[n-1].idCard" placeholder="身份证号" />
                  </el-form-item>
                </div>
              </el-form>
            </el-collapse-item>
          </el-collapse>
        </div>

        <!-- 联系人信息 -->
        <el-card shadow="hover" class="contact-card">
          <h3>联系人信息</h3>
          <el-form :model="contactForm" label-width="80px">
            <el-form-item label="姓名">
              <el-input v-model="contactForm.name" placeholder="姓名" />
            </el-form-item>
            <el-form-item label="手机">
              <el-input v-model="contactForm.phone" placeholder="手机号" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="contactForm.email" placeholder="邮箱" />
            </el-form-item>
          </el-form>
        </el-card>

        <!-- 特殊要求 -->
        <el-card shadow="hover" class="special-card">
          <h3>特殊要求</h3>
          <el-input
            type="textarea"
            v-model="specialRequest"
            placeholder="可填写额外需求"
            rows="3"
          />
        </el-card>
      </div>

      <!-- 右侧 -->
      <div class="right-panel">
        <el-card shadow="hover" class="payment-info">
          <h3>费用明细</h3>
          <div v-for="room in rooms" :key="room.id" class="detail-row">
            <span>{{ room.type }} (¥{{ room.price }} x {{ room.quantity }} x {{ nights }}晚)</span>
            <span>¥{{ room.price * room.quantity * nights }}</span>
          </div>

          <div class="detail-row">
            <span>优惠券</span>
            <el-select v-model="selectedCoupon" placeholder="选择优惠券">
              <el-option
                v-for="c in coupons"
                :key="c.id"
                :label="c.name"
                :value="c.discount"
              />
            </el-select>
          </div>

          <div class="total-row">
            <span>总价</span>
            <span class="total-amount">¥{{ totalAmount }}</span>
          </div>

          <el-button type="primary" class="submit-btn" @click="submitOrder">
            提交订单 (¥{{ totalAmount }})
          </el-button>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import { ElMessage } from "element-plus";

const hotel = ref({
  hotelName: "蒂凡尼酒店",
  address: "Colbjørnsensgade 28, Copenhagen, Denmark",
  hotelImg: "https://picsum.photos/300/200",
});

const nights = ref(1);

const rooms = ref([
  {
    id: 1,
    type: "标准间",
    price: 1000,
    guests: 2,
    quantity: 1,
    maxGuests: 3,
    active: ["1"],
    guestList: [
      { name: "", idCard: "" },
      { name: "", idCard: "" }
    ]
  },
  {
    id: 2,
    type: "高级间",
    price: 1500,
    guests: 2,
    quantity: 1,
    maxGuests: 4,
    active: [],
    guestList: [
      { name: "", idCard: "" },
      { name: "", idCard: "" }
    ]
  }
]);

// 当入住人数变化时，动态调整 guestList
rooms.value.forEach((room) => {
  watch(
    () => room.guests,
    (newVal) => {
      if (newVal > room.guestList.length) {
        for (let i = room.guestList.length; i < newVal; i++) {
          room.guestList.push({ name: "", idCard: "" });
        }
      } else if (newVal < room.guestList.length) {
        room.guestList.splice(newVal);
      }
    }
  );
});

const contactForm = ref({ name: "", phone: "", email: "" });
const specialRequest = ref("");

const coupons = ref([
  { id: 1, name: "满1000减100", discount: 100 },
  { id: 2, name: "满500减50", discount: 50 }
]);
const selectedCoupon = ref(0);

const totalAmount = computed(() => {
  let sum = rooms.value.reduce((acc, r) => acc + r.price * r.quantity * nights.value, 0);
  return sum - selectedCoupon.value;
});

const submitOrder = () => {
  ElMessage.success(`订单提交成功，总金额: ¥${totalAmount.value}`);
};
</script>

<style scoped>
.order-page {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 20px;
}

/* 顶部导航 */
.breadcrumb {
  margin-bottom: 20px;
}

/* 容器加宽并居中 */
.order-container {
  display: flex;
  gap: 20px;
  width: 1200px;   /* 页面整体加宽 */
  margin: 0 auto;  /* 居中 */
}

/* 左侧 */
.left-panel {
  flex: 2;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.hotel-info {
  display: flex;
  gap: 20px;
}
.hotel-img-wrapper {
  flex: 0 0 300px;
}
.hotel-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 6px;
}
.hotel-details h2 {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
}
.hotel-details p {
  margin: 6px 0;
  color: #666;
}

.room-card {
  margin-top: 10px;
}
.guest-info {
  margin-bottom: 10px;
  padding-left: 10px;
  border-left: 2px solid #e0e0e0;
}

/* 右侧 */
.right-panel {
  flex: 1;
  position: sticky;
  top: 20px;
  height: fit-content;
}
.payment-info {
  padding: 20px;
  background: #fff;
}
.detail-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.total-row {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
  font-size: 18px;
  margin: 15px 0;
}
.total-amount {
  color: #f56c6c;
}
.submit-btn {
  width: 100%;
}
</style>
 